#include "PrecompiledHeaders.h"
#include "MenuMain.h"

/*
 * Grab state pointers
 */
void MenuMain::getStatePointers()
{
    options = (MenuOptions *)((MenuState *)findByName("options"));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////// GAME STATE FUNCTIONS ///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void MenuMain::enter(void)
{
    // play menu bgm
    if(!resources->audio->isPlayingMenuBGM()) {
        resources->audio->playMenuBGM();
    }
    
    // set cameras to render through
    resources->render_window->getViewport(0)->setCamera(resources->main_gui_camera);
    if(resources->config->isTriScreen())
    {
        resources->render_window->getViewport(1)->setCamera(resources->left_gui_camera);
        resources->render_window->getViewport(2)->setCamera(resources->right_gui_camera);
    }
    
    // create a camera to render through
    Ogre::Camera *cam = resources->scene_manager->createCamera("gui_camera");
    resources->render_window->getViewport(0)->setCamera(cam);
    // Shows mouse
    CEGUI::MouseCursor::getSingleton().show();
    // Loads the MainMenu.layout script which places all of the main menu buttons on the screen
    CEGUI::Window* mainMenuSheet = CEGUI::WindowManager::getSingletonPtr()->loadWindowLayout("MainMenu.layout");
    resources->GUISystem->setGUISheet(mainMenuSheet);
    if(resources->pod_config_file)
        resources->GUIRenderer->setDisplaySize(CEGUI::Size(resources->config->getResolutionWidth() / 3, 0.8 * resources->config->getResolutionHeight()));
    else
        resources->GUIRenderer->setDisplaySize(CEGUI::Size(resources->config->getResolutionWidth(), resources->config->getResolutionHeight()));
    // Attaches events to the Main Menu buttons
    subscribeButton("MainMenu/NewGameButton",    &MenuMain::clickNewGame, &MenuMain::hoverNewGame, &MenuMain::offNewGame);
    subscribeButton("MainMenu/QuickPlayButton",  &MenuMain::clickPlay,    &MenuMain::hoverPlay,    &MenuMain::offPlay);
    subscribeButton("MainMenu/HighScoresButton", &MenuMain::clickScores,  &MenuMain::hoverScores,  &MenuMain::offScores);
    subscribeButton("MainMenu/OptionsButton",    &MenuMain::clickOptions, &MenuMain::hoverOptions, &MenuMain::offOptions);
    subscribeButton("MainMenu/QuitButton",       &MenuMain::clickQuit,    &MenuMain::hoverQuit,    &MenuMain::offQuit);
    // Initialises the menu index
    winIterator = activeWindows.begin();
}

// Exits the MenuState
void MenuMain::exit(void)
{
    CEGUI::MouseCursor::getSingleton().hide();
    CEGUI::WindowManager::getSingleton().destroyAllWindows();
	activeWindows.clear();
}

// Pushes on the new game state
bool MenuMain::clickNewGame(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("MainMenu/NewGameButton");
    pushGameState(findByName("story_mode_menu"));
    return true;
}

// Pushes on the level selection menu state
bool MenuMain::clickPlay(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("MainMenu/QuickPlayButton");
    pushGameState(findByName("quick_play_menu"));
    return true;
}

// Pushes on the high score menu state
bool MenuMain::clickScores(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("MainMenu/HighScoresButton");
    pushGameState(findByName("high_scores_menu"));
    return true;
}

// Pushes on the option menu state
bool MenuMain::clickOptions(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_CLICK);
    options->resetBackground();
    unsubscribeButton("MainMenu/OptionsButton");
    pushGameState(findByName("options"));
    return true;
}

// Quits the game
bool MenuMain::clickQuit(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_CLICK);
    unsubscribeButton("MainMenu/QuitButton");
    if(resources->pod_control) resources->pod->stop();
    popGameState();
    return true;
}

// Changes the appearance of the new game button to signify the mouse hovering over the button
bool MenuMain::hoverNewGame(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_OVER);
    setWindowText("MainMenu/NewGameButton", "[new game]");
    return true;
}

// Changes the appearance of the quickplay button to signify the mouse hovering over the button
bool MenuMain::hoverPlay(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_OVER);
    setWindowText("MainMenu/QuickPlayButton", "[quickplay]");
    return true;
}

// Changes the appearance of the high scores button to signify the mouse hovering over the button
bool MenuMain::hoverScores(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_OVER);
    setWindowText("MainMenu/HighScoresButton", "[high scores]");
    return true;
}

// Changes the appearance of the options button to signify the mouse hovering over the button
bool MenuMain::hoverOptions(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_OVER);
    setWindowText("MainMenu/OptionsButton", "[options]");
    return true;
}

// Changes the appearance of the quit button to signify the mouse hovering over the button
bool MenuMain::hoverQuit(const CEGUI::EventArgs &e)
{
    resources->audio->playSource(AUDIO_MENU_OVER);
    setWindowText("MainMenu/QuitButton", "[quit]");
    return true;
}

// Resets the appearance of the new game button to signify the mouse cursor leaving the button area
bool MenuMain::offNewGame(const CEGUI::EventArgs &e)
{
    setWindowText("MainMenu/NewGameButton", "new game");
    return true;
}

// Resets the appearance of the quickplay button to signify the mouse cursor leaving the button area
bool MenuMain::offPlay(const CEGUI::EventArgs &e)
{
    setWindowText("MainMenu/QuickPlayButton", "quickplay");
    return true;
}

// Resets the appearance of the high scores button to signify the mouse cursor leaving the button area
bool MenuMain::offScores(const CEGUI::EventArgs &e)
{
    setWindowText("MainMenu/HighScoresButton", "high scores");
    return true;
}

// Resets the appearance of the options button to signify the mouse cursor leaving the button area
bool MenuMain::offOptions(const CEGUI::EventArgs &e)
{
    setWindowText("MainMenu/OptionsButton", "options");
    return true;
}

// Resets the appearance of the quit button to signify the mouse cursor leaving the button area
bool MenuMain::offQuit(const CEGUI::EventArgs &e)
{
    setWindowText("MainMenu/QuitButton", "quit");
    return true;
}

